import pygame
import sys
import traceback
import myplane
import enemy
import bullet

pygame.init()
pygame.mixer.init()

BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN =(0,255,0)
size = width,height = 480,700
screen=pygame.display.set_mode(size)
######一定记得要写标题########
pygame.display.set_caption('飞机大战')
background=pygame.image.load('images/background.png').convert_alpha()


# 载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def add_smallenemy(group1,group2,num):
    for i in range(num):
        e1=enemy.Smallenemy(size)
        group1.add(e1)
        group2.add(e1)
def add_midenemy(group1,group2,num):
    for i in range(num):
        e2=enemy.Midenemy(size)
        group1.add(e2)
        group2.add(e2)
def add_bigenemy(group1,group2,num):
    for i in range(num):
        e3=enemy.Bigenemy(size)
        group1.add(e3)
        group2.add(e3)

def main():
    #各种初始化
    pygame.mixer.music.play(-1)
    clock= pygame.time.Clock()
    
    #生成我方飞机
    me=myplane.Myplane(size)
    #生成敌机组
    enemys = pygame.sprite.Group()
    #生成小飞机
    smallenemys = pygame.sprite.Group()
    add_smallenemy(enemys,smallenemys,15)
    #生成中飞机
    midenemys = pygame.sprite.Group()
    add_midenemy(enemys,midenemys,6)
    #生成大飞机
    bigenemys = pygame.sprite.Group()
    add_bigenemy(enemys,bigenemys,2)
    #生成子弹
    bullets = []
    bullet_num = 4
    bullet_index=0
    for i in range(bullet_num):
        bullets.append(bullet.Bullet1(me.rect.midtop))
    #统计得分
    score=0
    score_font = pygame.font.Font('font/font.ttf',36)
    #画所有
    draw=[]
    #暂停
    pause = False
    pause_nor_image=pygame.image.load('images/pause_nor.png').convert_alpha()
    pause_pressed_image=pygame.image.load('images/pause_pressed.png').convert_alpha()
    resume_nor_image=pygame.image.load('images/resume_nor.png').convert_alpha()
    resume_pressed_image=pygame.image.load('images/resume_pressed.png').convert_alpha()
    pause_image = pause_nor_image
    pause_rect=pause_nor_image.get_rect()
    pause_rect.left,pause_rect.top = (width-pause_rect.width-10),10
    #延时
    delay = 100
    
    switch = False
    runing=True
    while runing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1 and pause_rect.collidepoint(event.pos):
                    pause  =not pause
            elif event.type == pygame.MOUSEMOTION:
                if pause_rect.collidepoint(event.pos):
                    if pause:
                        pause_image = resume_pressed_image
                    else:
                        pause_image =pause_pressed_image
                else:
                    if pause:
                        pause_image = resume_nor_image
                    else:
                        pause_image =pause_nor_image
            
                        
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
            me.moveup()
        if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
            me.movedown()
        if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
            me.moveleft()
        if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
            me.moveright()
        #设定延时 5帧翻转一次
        if delay:
            delay-=1
        else:
            delay=100
        if not (delay%5):
            switch = not switch

        if not pause:
            screen.blit(background,(0,0))

            #画子弹_发射子弹
            if not (delay%10):
                bullets[bullet_index].reset(me.rect.midtop)
                bullet_index=(bullet_index+1)%bullet_num
            #画子弹_子弹飞行
            for each in bullets:
                if each.active:
                    each.move()
                    screen.blit(each.image,each.rect)
                    enemy_hit = pygame.sprite.spritecollide(each,enemys,False,\
                                                            pygame.sprite.collide_mask)
                    if enemy_hit:
                        each.active =False
                        for e in enemy_hit:
                            if e in midenemys or e in bigenemys:
                                e.hit = True
                                e.HP-=1
                                if e.HP == 0:
                                    e.active = False
                            else:
                                e.active = False
                else:
                    each.reset(me.rect.midtop)
                    
            #画大飞机
            for each in bigenemys:
                if each.active:
                    each.move()
                    pygame.draw.line(screen,BLACK,\
                        (each.rect.left,each.rect.top-5),\
                                     (each.rect.right,each.rect.top-5),2)
                    bl=each.HP/enemy.Bigenemy.HP
                    if bl > 0.2:
                        color = GREEN
                    else :
                        color = RED
                    pygame.draw.line(screen,color,\
                                     (each.rect.left,each.rect.top-5),\
                                     (each.rect.left+bl*each.rect.width,each.rect.top-5),2)
                    if each.rect.bottom == -50:
                        enemy3_fly_sound.play(-1)
                    if each.hit:
                        screen.blit(each.hit_image,each.rect)
                        each.hit=False
                    else:
                        if switch:
                            screen.blit(each.image1,each.rect)
                        else:
                            screen.blit(each.image2,each.rect)
                #毁灭
                else:
                    if not (delay%3):
                        if not (each.index%6):
                            enemy3_down_sound.play()
                        screen.blit(each.image_downs[each.index],each.rect)
                        each.index+=1
                        if not (each.index%6):
                            score+=each.score
                            each.reset()
                            enemy3_fly_sound.stop()
            #画中飞机
            for each in midenemys:
                if each.active:
                    each.move()
                    if each.hit :
                        screen.blit(each.hit_image,each.rect)
                        each.hit=False
                    else:
                        screen.blit(each.image,each.rect)
                    pygame.draw.line(screen,BLACK,\
                        (each.rect.left,each.rect.top-5),\
                                     (each.rect.right,each.rect.top-5),2)
                    bl=each.HP/enemy.Midenemy.HP
                    if bl > 0.2:
                        color = GREEN
                    else :
                        color = RED
                    pygame.draw.line(screen,color,\
                                     (each.rect.left,each.rect.top-5),\
                                     (each.rect.left+bl*each.rect.width,each.rect.top-5),2)
                #毁灭
                else:
                    if not (delay%3):
                        if not (each.index%4):
                            enemy2_down_sound.play()
                        screen.blit(each.image_downs[each.index],each.rect)
                        each.index+=1
                        if not (each.index%4):
                            score+=each.score
                            each.reset()
                
            #画小飞机
            for each in smallenemys:
                if each.active:
                    each.move()
                    screen.blit(each.image,each.rect)
                #毁灭
                else:
                    if not (delay%3):
                        if not (each.index%4):
                            enemy1_down_sound.play()
                        screen.blit(each.image_downs[each.index],each.rect)
                        each.index+=1
                        if not (each.index%4):
                            score+=each.score
                            each.reset()
            #检测碰撞
            enemys_down = pygame.sprite.spritecollide(me,enemys,False,pygame.sprite.collide_mask)
            if enemys_down:
                for i in enemys_down:
                    i.active = False
            #显示得分
            score_text=score_font.render('Score : %s'%str(score),True,WHITE)
            screen.blit(score_text,(10,5))
            #画自己的飞机
            if me.active:
                if switch:
                    screen.blit(me.image1,me.rect)
                else:
                    screen.blit(me.image2,me.rect)
            #毁灭
            else:
                if not (delay%3):
                    if not (each.index%4):
                        me_down_sound.play()
                    screen.blit(each.image_downs[each.index],each.rect)
                    each.index+=1
                    if not (each.index%4):
                        each.reset()
        screen.blit(pause_image,pause_rect)
        pygame.display.flip()
        clock.tick(60)


if __name__=='__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()
